package com.example.a001.part23;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

import com.example.a001.R;


public class NextActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);

        // 获取从 MainActivity 传来的数据
        Intent intent = getIntent();
        if (intent != null) {
            String data = intent.getStringExtra("key");
            Toast.makeText(this, "Received data: " + data, Toast.LENGTH_SHORT).show();
        }

        // 设置回传按钮点击事件
        Button backButton = findViewById(R.id.button_return_data);
        if (backButton != null) {
            backButton.setOnClickListener(v -> {
                Intent resultIntent = new Intent();
                resultIntent.putExtra("resultKey", "Hello from NextActivity");
                setResult(Activity.RESULT_OK, resultIntent);
                finish(); // 结束当前 Activity 并返回数据
            });
        } else {
            Toast.makeText(this, "Button not found", Toast.LENGTH_SHORT).show();
        }
    }
}
